/********************************************/
/*	Copyright (C) 2018 Gong Li Bin			*/
/*	Project:	GmRm2000-1.0.0				*/
/*	Author:		gong_libin					*/
/*	Date:		2018_07_01					*/
/*	File:		GmIP.go						*/
/********************************************/

package proto

import (
	"encoding/binary"
	"fmt"
)

type GmIP struct {
	IPv				uint
	IPhl			uint
	IPtos			uint8
	IPlen			uint16
	IPid			uint16
	IPoff			uint16
	IPttl			uint8
	IPp				uint8
	IPsum			uint16
	IPsrc, IPdst	string
}

func NewGmIP(buf []byte) *GmIP {
	src := uint(binary.BigEndian.Uint32(buf[12:16]))
	dst := uint(binary.BigEndian.Uint32(buf[16:20]))

	return &GmIP{
		IPv: uint(buf[0]) >> 4,
		IPhl: uint(buf[0]) & 0x0f,
		IPlen: uint16(binary.BigEndian.Uint16(buf[2:4])),
		IPttl: uint8(buf[8]),
		IPp: uint8(buf[9]),
		IPsum: uint16(binary.BigEndian.Uint16(buf[10:12])),
		IPsrc: fmt.Sprintf("%d.%d.%d.%d", byte(src >> 24), byte(src >> 16), byte(src >> 8), byte(src)),
		IPdst: fmt.Sprintf("%d.%d.%d.%d", byte(dst >> 24), byte(dst >> 16), byte(dst >> 8), byte(dst)),
	}
}

func (v GmIP)GmPrint() {
	fmt.Printf("v: %d, ", v.IPv)
	fmt.Printf("hl: %d, ", v.IPhl)
	fmt.Printf("len: %d, ", v.IPlen)
	fmt.Printf("ttl: %d, ", v.IPttl)
	fmt.Printf("p: 0x%02x, ", v.IPp)
	fmt.Printf("sum: 0x%04x\n", v.IPsum)
	fmt.Printf("src: %s, ", v.IPsrc)
	fmt.Printf("dst: %s\n", v.IPdst)
}